package cn.demo.common.core.page;


import cn.demo.common.constant.HttpStatus;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * 表格分页数据对象
 *
 * @author framework
 */
@Data
@NoArgsConstructor
public class PageR<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 总记录数
     */
    @Schema(description = "总记录数")
    private long total;

    /**
     * 列表数据
     */
    @Schema(description = "列表数据")
    private List<T> rows;

    /**
     * 消息状态码
     */
    @Schema(description = "消息状态码")
    private int code;

    /**
     * 消息内容
     */
    @Schema(description = "消息内容")
    private String msg;

    public static <T> PageR<T> ok(Page<T> page) {
        PageR<T> pageR = new PageR<>();
        pageR.setCode(HttpStatus.SUCCESS);
        pageR.setMsg("查询成功");
        pageR.setRows(null != page ? page.getRecords() : null);
        pageR.setTotal(null != page ? page.getTotalRow() : 0);
        return pageR;
    }

    public static <T> PageR<T> ok(List<T> list) {
        PageR<T> pageR = new PageR<>();
        pageR.setCode(HttpStatus.SUCCESS);
        pageR.setMsg("查询成功");
        pageR.setRows(list);
        pageR.setTotal(null != list ? list.size() : 0);
        return pageR;
    }

    public static <T> PageR<T> ok(List<T> list, Long total) {
        PageR<T> pageR = new PageR<>();
        pageR.setCode(HttpStatus.SUCCESS);
        pageR.setMsg("查询成功");
        pageR.setRows(list);
        pageR.setTotal(total);
        return pageR;
    }

    public static <T> PageR<T> fail(String msg) {
        PageR<T> pageR = new PageR<>();
        pageR.setCode(HttpStatus.ERROR);
        pageR.setMsg(msg);
        pageR.setRows(null);
        pageR.setTotal(0);
        return pageR;
    }
}
